package com.gitee.wsl.data.recycler

import com.gitee.wsl.data.recycler.RecyclerPool.WithPool


/**
 * [RecyclerPool] implementation that does not use
 * any pool but simply creates new instances when necessary.
 */
abstract class NonRecyclingPoolBase<P : WithPool<P>> : RecyclerPool<P> {
    // // // Actual API implementation
    override fun acquireAndLinkPooled(): P {
        // since this pool doesn't do anything on release it doesn't need to be registered on the BufferRecycler
        return acquirePooled()
    }

    abstract override fun acquirePooled(): P

    override fun releasePooled(pooled: P) {
        // nothing to do, there is no underlying pool
    }

    override fun pooledCount(): Int {
        return 0
    }

    /**
     * Although no pooling occurs, we consider clearing to succeed,
     * so returns always `true`.
     *
     * @return Always returns `true`
     */
    override fun clear(): Boolean {
        return true
    }

    companion object {
        private const val serialVersionUID = 1L
    }
}

fun <P : WithPool<P>> RecyclerPool.Companion.nonRecycling(createPooled:()->P)
        = object :NonRecyclingPoolBase<P>(){

    override fun acquirePooled(): P = createPooled()
}
